package com.project.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    @Autowired
    CachingConnectionFactory connectionFactory;
    //rabbitmq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate(){
        return new RabbitTemplate(connectionFactory);
    }
    //创建队列
    //采购申请
    @Bean
    public Queue queueOne(){
        return new Queue("BackPurchaseApplyOne");
    }
    //供应商退货
    @Bean
    public Queue queueTwo(){
        return new Queue("BackRejectedApplyTwo");
    }
    //促销商品审核
    @Bean
    public Queue queueThree(){
        return new Queue("PromotionReviewThree");
    }
    //创建交换机
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange("BackPurchaseApply");
    }
    //交换机与队列绑定
    //采购申请
    @Bean
    public Binding directExchangeToQueueOne(Queue queueOne, DirectExchange directExchange){
        return   BindingBuilder.bind(queueOne).to(directExchange).with("purchase.key");
    }
    //供应商退货
    @Bean
    public Binding directExchangeToQueueTwo(Queue queueTwo, DirectExchange directExchange){
        return   BindingBuilder.bind(queueTwo).to(directExchange).with("rejected.key");
    }
    //促销商品审核
    @Bean
    public Binding directExchangeToQueueThree(Queue queueThree, DirectExchange directExchange){
        return   BindingBuilder.bind(queueThree).to(directExchange).with("Review.key");
    }
}
